<template>
 <div style="height: 200px">
    <el-form :model="addDailyForm"  label-width="120px">
      <el-col :span="12"  v-for="(domain, index) in addDailyForm.changerMessage" :key="index">
        <el-form-item
            :label="`业务组织角色${index + 1}:`"
            :prop="'changerMessage.' + index + '.value'"
            :rules="[{ required: true, message: '必填项不能为空' }]"
        >
          <div style="display: flex;">
            <el-input  :disabled="disableds"  readonly placeholder="请选择" v-model="domain.value" ></el-input>
            <i  @click="showClick" style="position: absolute; right: 27px;top: 8px;font-size: 20px;cursor: pointer;"  id="namun" class="el-icon-menu"></i>
            <el-link  :underline="false" @click.prevent="removeDomain(domain)"><i class="el-input__icon el-icon-remove-outline"></i></el-link>
          </div>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-button  type="primary" @click='handleClick'>新增业务组织角色</el-button>
      </el-col>
    </el-form>
   <el-dialog
       title="提示"
       :visible.sync="dialogVisible"
       width="30%"
       >
     <el-table
         ref="multipleTable"
         :data="tableData"
         tooltip-effect="dark"
         style="width: 100%"
       >
       <el-table-column
           type="selection"
           width="55">
       </el-table-column>
       <el-table-column
           label="日期"
           width="120">
         <template slot-scope="scope">{{ scope.row.date }}</template>
       </el-table-column>
       <el-table-column
           prop="name"
           label="姓名"
           width="120">
       </el-table-column>
       <el-table-column
           prop="address"
           label="地址"
           show-overflow-tooltip>
       </el-table-column>
     </el-table>
     <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="onclick">确 定</el-button>
  </span>
   </el-dialog>
 </div>
</template>

<script>
export default {
  name: "formIndex",
  data(){
    return{
      disableds:false,
      dialogVisible:false,
      addDailyForm: {
        changerMessage: [
          {
            value: "",
          },
        ],
      },
      tableData: [{
        date: '2016-05-03',
        name: '张三',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-02',
        name: '李四',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '小黑',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-08',
        name: '小白',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-06',
        name: '小红',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-07',
        name: '王大虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }],
    }
  },
  methods:{
    showClick(){
        this.dialogVisible=true
    },
    removeDomain(item){
      // 必须留一项
      if (this.addDailyForm.changerMessage.length === 1) {
        return;
      }
      let index = this.addDailyForm.changerMessage.indexOf(item)
      if (index !== -1) {
        this.addDailyForm.changerMessage.splice(index, 1)
      }
    },
    handleClick(){
      this.addDailyForm.changerMessage.push({
        value: '',
        key: Date.now()
      })
    },
    onclick(){
      console.log(this.$refs.multipleTable)
    //  判断只能选择一条数据
      if(this.$refs.multipleTable.selection.length !== 1){
        this.$message({
          message: '只能选择一条数据',
          type: 'warning'
        });
        return
      }
      let selectData = this.$refs.multipleTable.selection
      //深拷贝
      let arr = JSON.parse(JSON.stringify(this.addDailyForm.changerMessage))
      arr.forEach((item)=>{
        item.value = selectData[0].name
      })
      this.$set(this.addDailyForm,'changerMessage',arr)
      this.dialogVisible=false
    }
  }
}
</script>

<style scoped>

</style>